package com.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.controller.dkjdcontroller;
import com.util.BODY;
import com.util.HttpRequest;
import com.util.IpUtil;
import com.util.JSONUtil;
import com.util.MD5;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

public class KEY {
	private static final Logger logger = Logger.getLogger(dkjdcontroller.class);
	Map map = new HashMap();
	List list = new ArrayList();

	public String ptdl(HttpServletResponse response, HttpSession session,HttpServletRequest request) throws ClientProtocolException, IOException {
			SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		String interfaceUrl = ReadConfig.readValue();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String Key="";
		if(sessionIn!=null){
			if(!(sessionIn.getKEY().equals("")|| sessionIn.getKEY()==null)){
				 Key=sessionIn.getKEY();
			}
		}
		String a="";
		String Timestamp=sdf.format(date);
		String jm="123456"+Timestamp+"y1uUuBiikf0a5FHZ";
		// 接口编码
		System.out.println(jm);
		MD5 md5=new MD5();
		a=md5.getMD5(jm).toString();
		if(sessionIn==null){
			body.setPassWd(a);
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin");
				remap.put("BODY", body);
				body.setTimestamp(Timestamp);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				try {
					if(retMap2.get("RETURNCODE").equals("100")){
						Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
						List jx = (List) mx2.get("RETDATA");
						Map m = (Map) jx.get(0);
						Key += m.get("OpenKey").toString();		
					}else{
						map.put("code", 1);
						map.put("CW", retMap2.get("RETURNDESC").toString());
						list.add(retMap2);
						response.setHeader("Content-type", "text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
						return null;
					}
				} catch (Exception e) {
					map.put("code", 1);
					map.put("CW", "登录失败！");
					list.add(retMap2);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
					return null;
				}
				
				
		}else if(sessionIn.getKEY()==null){
			body.setPassWd(a);
			remap.put("AUTHORITY", "YBGJJ100");
			// 接口参数
			remap.put("LOGUserName", "admin");
			remap.put("BODY", body);
			String json3 = JSONUtil.ToJsongString2(remap);
			// 请求
			String result2 = HttpRequest.doPost(interfaceUrl, json3);
			logger.info("接口返回内容:" + result2);
			Map retMap2 = JSONUtil.toMap(result2);
			if(retMap2.get("RETURNCODE").equals("100")){
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key +=m.get("OpenKey").toString();		
			}else{
				map.put("code", 1);
				map.put("CW", retMap2.get("RETURNDESC").toString());
				list.add(retMap2);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
			
		}
		return jm;
	}
}